Game xếp hình Tetris C#
- MatrizPantalla.cs
- project /
1 using System;
2
3 namespace Tetris
4 {
5 /// <summary>
6 /// Descripción breve de MatrizPantalla.
7 /// </summary>
8 public class MatrizPantalla
9 {
10 int [, ] m_matrizPantalla;
11
12 public MatrizPantalla()
13 {
14 m_matrizPantalla = new int[Constantes.FILAS_PANTALLA, Constantes.COLUMNAS_PANTALLA];
15 }
16
17 public int [, ]matrizPantalla
18 {
19 get
20 {
21 return m_matrizPantalla;
22 }
23 }
24
25 public int this [int y, int x]
26 {
27 get
28 {
29 if (x < 0 || x > Constantes.COLUMNAS_PANTALLA || y < 0 || y > Constantes.FILAS_PANTALLA)
30 throw new Exception("El intervalo está fuera del índice");
31 else
32 return m_matrizPantalla[y, x];
33 }
34 set
35 {
36 if (!(x < 0 || x > Constantes.COLUMNAS_PANTALLA || y < 0 || y > Constantes.FILAS_PANTALLA))
37 m_matrizPantalla[y, x] = value;
38 else
39 throw new Exception("El intervalo está fuera del índice");
40 }
41 }
42
43 public void pintarPieza(Pieza p)
44 {
45 int ancho = p.ancho;
46 int alto = p.alto;
47 for (int x = 0; x < ancho; x++)
48 {
49 for (int y = 0; y < alto; y++)
50 {
51 if (p[y, x] == 1 && p.posY + y >= 0)
52 {
53 m_matrizPantalla[p.posY + y, p.posX + x] = p.color;
54 }
55 }
56 }
57 }
58
59 public bool puedeBajarPieza(Pieza p)
60 {
61 bool puede = true;
62 int ancho = p.ancho;
63 int alto = p.alto;
64 if (p.posY + p.alto > Constantes.FILAS_PANTALLA || hayColision(p))
65 {
66 puede = false;
67 }
68 return puede;
69 }
70
71 public bool hayColision(Pieza p)
72 {
73 bool hay = false;
74 int ancho = p.ancho;
75 int alto = p.alto;
76 for (int x = 0; x < ancho; x++)
77 {
78 for (int y = 0; y < alto; y++)
79 {
80 if (p[y, x] == 1 && p.posY + y >= 0)
81 {
82 if (m_matrizPantalla[p.posY + y, p.posX + x] != 0)
83 {
84 hay = true;
85 }
86 }
87 }
88 }
89 return hay;
90 }
91
92 public bool puedeMoverPieza(Pieza p)
93 {
94 bool puede = true;
95 int ancho = p.ancho;
96 int alto = p.alto;
97 if (p.posX < 0 || p.posX + p.ancho > Constantes.COLUMNAS_PANTALLA || hayColision(p))
98 {
99 puede = false;
100 }
101 return puede;
102 }
103
104 public void borrarPieza(Pieza p)
105 {
106 int ancho = p.ancho;
107 int alto = p.alto;
108 for (int x = 0; x < ancho; x++)
109 {
110 for (int y = 0; y < alto; y++)
111 {
112 if (p[y, x] == 1 && p.posY + y >= 0)
113 {
114 m_matrizPantalla[p.posY + y, p.posX + x] = 0;
115 }
116 }
117 }
118 }
119
120 public void borrarPantalla()
121 {
122 m_matrizPantalla = new int[Constantes.FILAS_PANTALLA, Constantes.COLUMNAS_PANTALLA];
123 }
124
125 public int eliminaLineasCompletas()
126 {
127 bool filaCompleta = true;
128 int numFilasCompletas = 0;
129 int x;
130 for (int y = 0; y < Constantes.FILAS_PANTALLA; y++)
131 {
132 filaCompleta = true;
133 x = 0;
134 while (x < Constantes.COLUMNAS_PANTALLA && filaCompleta)
135 {
136 if (m_matrizPantalla[y, x] == 0)
137 {
138 filaCompleta = false;
139 }
140 x++;
141 }
142 if (filaCompleta)
143 {
144 eliminaLinea(y);
145 numFilasCompletas++;
146 }
147 }
148 return numFilasCompletas;
149 }
150
151 private void eliminaLinea(int linea)
152 {
153 for (int y = linea; y > 0; y--)
154 {
155 for (int x = 0; x < Constantes.COLUMNAS_PANTALLA; x++)
156 {
157 m_matrizPantalla[y, x] = m_matrizPantalla[y - 1, x];
158 }
159 }
160 }
161 }
162 }
2
3 namespace Tetris
4 {
5 /// <summary>
6 /// Descripción breve de MatrizPantalla.
7 /// </summary>
8 public class MatrizPantalla
9 {
10 int [, ] m_matrizPantalla;
11
12 public MatrizPantalla()
13 {
14 m_matrizPantalla = new int[Constantes.FILAS_PANTALLA, Constantes.COLUMNAS_PANTALLA];
15 }
16
17 public int [, ]matrizPantalla
18 {
19 get
20 {
21 return m_matrizPantalla;
22 }
23 }
24
25 public int this [int y, int x]
26 {
27 get
28 {
29 if (x < 0 || x > Constantes.COLUMNAS_PANTALLA || y < 0 || y > Constantes.FILAS_PANTALLA)
30 throw new Exception("El intervalo está fuera del índice");
31 else
32 return m_matrizPantalla[y, x];
33 }
34 set
35 {
36 if (!(x < 0 || x > Constantes.COLUMNAS_PANTALLA || y < 0 || y > Constantes.FILAS_PANTALLA))
37 m_matrizPantalla[y, x] = value;
38 else
39 throw new Exception("El intervalo está fuera del índice");
40 }
41 }
42
43 public void pintarPieza(Pieza p)
44 {
45 int ancho = p.ancho;
46 int alto = p.alto;
47 for (int x = 0; x < ancho; x++)
48 {
49 for (int y = 0; y < alto; y++)
50 {
51 if (p[y, x] == 1 && p.posY + y >= 0)
52 {
53 m_matrizPantalla[p.posY + y, p.posX + x] = p.color;
54 }
55 }
56 }
57 }
58
59 public bool puedeBajarPieza(Pieza p)
60 {
61 bool puede = true;
62 int ancho = p.ancho;
63 int alto = p.alto;
64 if (p.posY + p.alto > Constantes.FILAS_PANTALLA || hayColision(p))
65 {
66 puede = false;
67 }
68 return puede;
69 }
70
71 public bool hayColision(Pieza p)
72 {
73 bool hay = false;
74 int ancho = p.ancho;
75 int alto = p.alto;
76 for (int x = 0; x < ancho; x++)
77 {
78 for (int y = 0; y < alto; y++)
79 {
80 if (p[y, x] == 1 && p.posY + y >= 0)
81 {
82 if (m_matrizPantalla[p.posY + y, p.posX + x] != 0)
83 {
84 hay = true;
85 }
86 }
87 }
88 }
89 return hay;
90 }
91
92 public bool puedeMoverPieza(Pieza p)
93 {
94 bool puede = true;
95 int ancho = p.ancho;
96 int alto = p.alto;
97 if (p.posX < 0 || p.posX + p.ancho > Constantes.COLUMNAS_PANTALLA || hayColision(p))
98 {
99 puede = false;
100 }
101 return puede;
102 }
103
104 public void borrarPieza(Pieza p)
105 {
106 int ancho = p.ancho;
107 int alto = p.alto;
108 for (int x = 0; x < ancho; x++)
109 {
110 for (int y = 0; y < alto; y++)
111 {
112 if (p[y, x] == 1 && p.posY + y >= 0)
113 {
114 m_matrizPantalla[p.posY + y, p.posX + x] = 0;
115 }
116 }
117 }
118 }
119
120 public void borrarPantalla()
121 {
122 m_matrizPantalla = new int[Constantes.FILAS_PANTALLA, Constantes.COLUMNAS_PANTALLA];
123 }
124
125 public int eliminaLineasCompletas()
126 {
127 bool filaCompleta = true;
128 int numFilasCompletas = 0;
129 int x;
130 for (int y = 0; y < Constantes.FILAS_PANTALLA; y++)
131 {
132 filaCompleta = true;
133 x = 0;
134 while (x < Constantes.COLUMNAS_PANTALLA && filaCompleta)
135 {
136 if (m_matrizPantalla[y, x] == 0)
137 {
138 filaCompleta = false;
139 }
140 x++;
141 }
142 if (filaCompleta)
143 {
144 eliminaLinea(y);
145 numFilasCompletas++;
146 }
147 }
148 return numFilasCompletas;
149 }
150
151 private void eliminaLinea(int linea)
152 {
153 for (int y = linea; y > 0; y--)
154 {
155 for (int x = 0; x < Constantes.COLUMNAS_PANTALLA; x++)
156 {
157 m_matrizPantalla[y, x] = m_matrizPantalla[y - 1, x];
158 }
159 }
160 }
161 }
162 }